package com.gxa.clientkill.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.clientkill.pojo.Ticket;
import com.gxa.clientkill.service.OrderService;
import com.gxa.clientkill.service.TicketService;
import com.gxa.clientkill.utils.JsonResult;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Jun
 * @version 1.0
 * @date 2020/11/11 10:18
 */

@Api(value = "实现优惠券功能的控制器",tags = "优惠券接口的api")
@RestController
@RequestMapping("/tkt")
public class TicketController {

    @Resource
    TicketService ticketService;

    @Resource
    OrderService orderService;

    JsonResult jsonResult = new JsonResult();

    @ApiOperation(value = "修改接口", notes = "优惠券的修改", httpMethod = "POST", response = Map.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @PostMapping("/update")
    public Map update(@RequestBody @ApiParam(name = "kill",value = "优惠券的pojo信息",required = true) Ticket ticket) {
        Integer update = ticketService.update(ticket);
        jsonResult.setData(ticket);
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getValues();
    }


    @PostMapping("/list")
    @ApiOperation(value = "分页查询接口", notes = "用于优惠券的列表展示", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "limit", value = "分页大小", required = true, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页", required = true, dataType = "int", paramType = "query")
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map list(@RequestParam("limit") int limit, @RequestParam("page") int page) {
        //定义一个map
        Map<String, Object> ticketMap = new HashMap<>();
        //将分页条件输入PageHelper
        Page<Object> page1 = PageHelper.startPage(page, limit);
        //将所有的优惠券信息查询出来
        List<Ticket> tickets = ticketService.list();
        //将分页信息与所有的优惠券信息结合
        PageInfo<Ticket> info = new PageInfo<>(tickets);
        //将分页的列表导入map中
        ticketMap.put("list",info.getList());
        //将总记录数导入map中
        ticketMap.put("counts",info.getTotal());
        ticketMap.put("pages",info.getTotal()/limit);
        jsonResult.put("counts",ticketMap.get("counts"));
        jsonResult.setData(ticketMap.get("list"));
        jsonResult.setCode("200");
        jsonResult.setMsg("分页查询成功");
        return jsonResult.getValues();
    }

    @PostMapping("/add")
    @ApiOperation(value = "优惠券添加接口", notes = "该接口用于添加不同类型优惠券", httpMethod = "POST", response = Map.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map add(@RequestBody @ApiParam(name = "ticket",value = "优惠券活动的pojo信息",required = true) Ticket ticket) {
        ticketService.add(ticket);
        jsonResult.setData(ticket);
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getValues();
    }

    @PostMapping("/del")
    @ApiOperation(value = "优惠券删除", notes = "该接口用于删除优惠券", httpMethod = "POST", response = Map.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "优惠券id", required = true, dataType = "int", paramType = "query"),
    })
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    public Map del(@RequestParam("id") int id) {
        Ticket ticket = ticketService.sel(id);
        if (ticket.getState()==1){
            jsonResult.setData("");
            jsonResult.setCode("500");
            jsonResult.setMsg("该优惠券已发放，无法删除");
            return jsonResult.getValues();
        }else {
            Integer del = ticketService.del(id);
            jsonResult.setData(del);
            jsonResult.setCode("200");
            jsonResult.setMsg("删除成功");
            return jsonResult.getValues();
        }
    }

    @ApiOperation(value = "发放接口", notes = "优惠券的发放", httpMethod = "POST", response = Map.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 404, message = "找不到页面"),
            @ApiResponse(code = 415, message = "数据类型格式不正确"),
            @ApiResponse(code = 500, message = "后台服务错误")
    })
    @PostMapping("/send")
    public Map send(@RequestParam("id") int id) {
        Integer send = ticketService.send(id);
        jsonResult.setData(send);
        jsonResult.setCode("200");
        jsonResult.setMsg("发放成功");
        return jsonResult.getValues();
    }

    @PostMapping("/use")
    public Map use(@RequestParam("ordnum") String ordnum){
        Map<String,Object> map = orderService.userOrddetail(ordnum);
        Map<String,Object> date = (Map<String, Object>) map.get("data");
        System.out.println(date);
        Double money = (Double) date.get("money");

        date.get("id");

        jsonResult.setData(date);
        jsonResult.setMsg("");
        return jsonResult.getValues();
    }
}
